package com.yanqu.road.server.cmd.unionwar;

import com.yanqu.road.entity.activity.unionwar.cross.CrossUnionWarSimple;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.unionwar.UnionWarActivityServerProto;
import com.yanqu.road.server.cmd.CrossUnionCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.union.activitygroup.Cross2UnionActivityGroupMgr;
import com.yanqu.road.server.manager.unionwar.CrossUnionWarMgr;
import com.yanqu.road.server.manager.unionwar.CrossUnionWarRankMgr;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.utils.string.StringUtils;

;

@Cmd(code = CrossProtocol.C_UNION_WAR_UNION_SCORE_RANK, desc = "过程中商会积分排名")
public class CrosssUnionWarUnionScoreAndRankCmd extends CrossUnionCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {

        UnionWarActivityServerProto.UnionScoreAndRankReqMsg reqMsg = UnionWarActivityServerProto.UnionScoreAndRankReqMsg.parseFrom(packet.getBytes());
        int activityId = reqMsg.getActivityId();
        int phase = reqMsg.getPhase();

        String unionUid = Cross2UnionActivityGroupMgr.getUnionUid(activityId, packet.getPlayerId());
        if(StringUtils.isNullOrEmpty(unionUid)){
            unionUid = reqMsg.getUnionUid();
        }
        UnionWarActivityServerProto.UnionScoreAndRankRespMsg.Builder respMsg = UnionWarActivityServerProto.UnionScoreAndRankRespMsg.newBuilder();
        respMsg.setRet(0);
        respMsg.setActivityId(activityId);
        respMsg.setPhase(phase);
        respMsg.setRank(-1);
        respMsg.setScore(0);

        CrossUnionWarSimple simple = CrossUnionWarMgr.getCrossUnionWarSimple(activityId, unionUid);
        if(simple != null){
            if(phase == 1){
                respMsg.setScore(simple.getScore1());
                respMsg.setRank(simple.getNo());
            }else if(phase == 2){
                respMsg.setScore(simple.getScore2());
                int rank = CrossUnionWarRankMgr.getUnionWarRank(activityId, 1001, serverId, unionUid, packet.getPlayerId());
                respMsg.setRank(rank);
            }
        }

        MessageHelper.sendPacket(serverId, packet.getPlayerId(), YanQuMessageUtils.buildMessage(Protocol.U_UNION_WAR_UNION_SCORE_RANK, respMsg));
    }

    @Override
    public void sendData() {

    }
}
